
package next.view.support;

import java.util.ArrayList;

import next.TemplateActivity.R;
import android.content.Context;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class Products_ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable, ContextMenuInfo {

	 private Filter filter;
	 private boolean CODE_ACTIVATE=false;
	 
	public void activateCODE()
	{
		CODE_ACTIVATE=!CODE_ACTIVATE;
	}
	 public boolean isCodeActivate ()
	 {
		 return CODE_ACTIVATE;
	 }
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;

    
    private ArrayList<S_3R4_ObjectType> groups, groupItems=null;
    private ArrayList<ArrayList<S_3R4_Object>> children,childItems=null, Groupfiltered;

  

    public Products_ExpandableListAdapter(Context context2,
			ArrayList<S_3R4_ObjectType> arrayList,
			ArrayList<ArrayList<S_3R4_Object>> children2) {
    	this.context = context2;
        this.groups = arrayList;
        this.children = children2;
	}
	public void addItem(S_3R4_Object S_3R4_Object, S_3R4_ObjectType S_3R4_ObjectType ) {
        if (!groups.contains(S_3R4_ObjectType)) {
            groups.add(S_3R4_ObjectType);
        }
        int index = groups.indexOf(S_3R4_ObjectType);
        if (children.size() < index + 1) {
            children.add(new ArrayList<S_3R4_Object>() );
        }
        children.get(index).add(S_3R4_Object);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	
        S_3R4_Object object = (S_3R4_Object) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.generic_elv_child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.childlayout_TV1);
        tv.setText("   " + object.getChain1());
        
        ImageView iv = (ImageView) convertView.findViewById(R.id.childlayout_IG01);
        //iv.setImageResource(Refs.refs.getImagesIds().get(object.getImages_idImages()));
        
        TextView tvDetail = (TextView) convertView.findViewById(R.id.childlayout_TV1);
        tv.setText("   " + object.getChain2());
        convertView.setTag(object);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        S_3R4_ObjectType group = (S_3R4_ObjectType) getGroup(groupPosition);
       
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.generic_elv_group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.grouplayout_TV01);
        tv.setText(group.getTypename());
        
        ImageView iv = (ImageView) convertView.findViewById(R.id.grouplayout_IG01);
        //iv.setBackgroundResource(resid);
        convertView.setTag(group);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

   @Override
    public Filter getFilter()
    {
        if(filter == null)
            filter = new ExpandableListFilter();
        return filter;
    }
    
	private class ExpandableListFilter extends Filter
	{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        // NOTE: this function is *always* called from a background thread, and
        // not the UI thread.
    	if (childItems==null){
    		childItems = new ArrayList<ArrayList<S_3R4_Object>>();

         	for (int i = 0; i < children.size(); i++) {
         		ArrayList<S_3R4_Object> aux=new ArrayList<S_3R4_Object>();
        		for (int j = 0; j < children.get(i).size(); j++) {
    				aux.add(children.get(i).get(j));
    			}
         		childItems.add (aux);
			}
    	}
    	
     	if (groupItems==null){
    	  //Assuming all Categories
        groupItems=new ArrayList<S_3R4_ObjectType>();
        for (int i = 0; i < groups.size(); i++) {
			groupItems.add(groups.get(i));
		}
     	}
     	
    	constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
       
        if(constraint != null && constraint.toString().length() > 0)
        {
        	
        	ArrayList<ArrayList<S_3R4_Object>> lItems;
            synchronized (childItems)
            {
                 	lItems = new ArrayList<ArrayList<S_3R4_Object>>();
                  	for (int i = 0; i < childItems.size(); i++) {
                 		ArrayList<S_3R4_Object> aux=new ArrayList<S_3R4_Object>();
                		for (int j = 0; j < childItems.get(i).size(); j++) {
                			
                			if (CODE_ACTIVATE){
                			if ( (childItems.get(i).get(j).getChain2()).toLowerCase().contains(constraint))
            				aux.add(childItems.get(i).get(j));
                			}
                			else
                			{
                				if ( (childItems.get(i).get(j).getChain1()).toLowerCase().contains(constraint))
                    				aux.add(childItems.get(i).get(j));
                        				
                			}
            			}
                  		lItems.add ( aux);
        			}
            }
           
           /* int suma=0;
            for (int i = 0; i < lItems.size(); i++) {
            	suma+=lItems.get(i).size();
			}*/
            result.count = lItems.size();
            result.values = lItems;
          //  Toast.makeText(context, items.size()+" p", Toast.LENGTH_SHORT).show();
        }
        else
        {
            synchronized(childItems)
            {
                result.values = childItems;
                result.count = childItems.size();
            }
           // Toast.makeText(context, result.count+" b", Toast.LENGTH_SHORT).show();
        }
        return result;
    }

  
	@SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // NOTE: this function is *always* called from the UI thread.
		Groupfiltered = (ArrayList<ArrayList<S_3R4_Object>>)results.values;
    	 if (Groupfiltered!=null){
        
    		 groups.clear();
    		 children.clear();
    		 groups= new ArrayList<S_3R4_ObjectType>();
    		 children= new ArrayList<ArrayList<S_3R4_Object>>();
    		 //checking categories
             for (int i = 0; i < groupItems.size(); i++) {
				if (Groupfiltered.get(i).size()>0){
					groups.add(groupItems.get(i));
					//for (int k = 0; k < Groupfiltered.get(i).size(); k++) {
		            	 children.add ( new ArrayList<S_3R4_Object>( Groupfiltered.get(i) ));
		 			//}
				}
			}
     
        notifyDataSetChanged();
    	 }else  notifyDataSetInvalidated();
    }
    
  }

	
}